package com.witmore.skyline.web.service.messagehandler.handler;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.witmore.skyline.db.entity.DeviceDO;
import com.witmore.skyline.db.mapper.DeviceMapper;
import com.witmore.skyline.web.controller.api.CloudmMessageParam;
import com.witmore.skyline.web.service.CameraService;
import com.witmore.skyline.web.service.messagehandler.MessageHandler;
import com.witmore.skyline.web.service.messagehandler.MessageType;
import com.witmore.skyline.web.service.third.yjx.dto.CameraDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description: CameraInfoUpdateHandler
 * Author: 豆子高
 * Date: 2025/4/18 10:17
 */
@MessageHandlesType(MessageType.cameraUpdate)
@Slf4j
@Service
public class CameraInfoUpdateHandler implements MessageHandler {

    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private CameraService cameraService;

    @Override
    public void handlerMessage(CloudmMessageParam param) {
        log.info("CameraInfoUpdateHandler 接收到消息：{}", param);
        // 查询设备信息
        DeviceDO deviceDO = deviceMapper.selectOne(new LambdaQueryWrapper<DeviceDO>()
                .eq(DeviceDO::getDeviceSn, param.getSn())
                .eq(DeviceDO::getIsDeleted, 0)
        );
        if (deviceDO == null) {
            log.info("设备不存在, sn: {}", param.getSn());
            return;
        }
        String content = param.getContent();
        List<CameraDTO> cameraDTOS = JSONObject.parseArray(content, CameraDTO.class);
        if (ObjectUtils.isEmpty(cameraDTOS)){
            return;
        }
        // 摄像头信息入库
        cameraService.getAndSaveCameraList(deviceDO, cameraDTOS);
    }
}
